﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using AudioCore.DAO;
using AudioCore.Entity;

namespace AudioCore.Biz
{
    public class FavoriteBiz : BaseBiz
    {
        public int AddFavorite(FavoriteEntity entity)
        {
            try
            {
                using (FavoriteDao dao = new FavoriteDao())
                {
                    return dao.InsertEntity(entity, typeof(FavoriteEntity));
                }
            }
            catch (Exception ex)
            {
            }
            return 0;
        }

        public int UpdateFavorite(FavoriteEntity entity)
        {
            try
            {
                using (FavoriteDao dao = new FavoriteDao())
                {
                    return dao.UpdateEntity(entity, typeof(FavoriteEntity));
                }
            }
            catch (Exception ex)
            {
            }
            return 0;
        }

        public int DeleteFavorite(int favoriteID)
        {
            try
            {
                using (FavoriteDao dao = new FavoriteDao())
                {
                    return dao.DeleteEntity(favoriteID);
                }
            }
            catch (Exception ex)
            {
            }
            return 0;
        }

        public FavoriteEntity GetFavoriteByID(int favoriteID)
        {
            try
            {
                using (FavoriteDao dao = new FavoriteDao())
                {
                    DataTable table = dao.SelectEntityByID(favoriteID);
                    if (table.Rows.Count > 0)
                    {
                        return (FavoriteEntity)BaseBiz.MapDataRowToObject(table.Rows[0], typeof(FavoriteEntity));
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return null;
        }

        public List<FavoriteEntity> GetAllFavorite()
        {
            try
            {
                using (FavoriteDao dao = new FavoriteDao())
                {
                    DataTable table = dao.SelectAllItem();
                    if (table.Rows.Count > 0)
                    {
                        return BaseBiz.MapDataTableToList(table, typeof(FavoriteEntity)).ConvertAll(item => (FavoriteEntity)item);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return new List<FavoriteEntity>();
        }

        public int DeleteFavoriteByAccountID(int accountID, int itemType)
        {
            try
            {
                using (FavoriteDao dao = new FavoriteDao())
                {
                    string[] columns = new string[] { "AccountID", "ItemTypeID" };
                    object[] values = new object[] { accountID, itemType };
                    return dao.DeleteItemByColumnNames(columns, values);
                }
            }
            catch (Exception ex)
            {
            }
            return -1;
        }

        public int DeleteFavorite(int accountID, int articleID, int itemType)
        {
            try
            {
                using (FavoriteDao dao = new FavoriteDao())
                {
                    string[] columns = new string[] { "AccountID", "ItemID", "ItemTypeID" };
                    object[] values = new object[] { accountID, articleID, itemType };
                    return dao.DeleteItemByColumnNames(columns, values);
                }
            }
            catch (Exception ex)
            {
            }
            return -1;
        }

        public bool CheckExistItemID(int accountID, int articleID, int itemType)
        {
            try
            {
                using (FavoriteDao dao = new FavoriteDao())
                {
                    string[] columns = new string[] {"AccountID", "ItemID", "ItemTypeID" };
                    object[] values = new object[] { accountID, articleID, itemType };
                    DataTable table = dao.SelectEntityByColumnNames(columns, values);
                    if (table.Rows.Count > 0)
                    {
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return false;
        }
    }
}
